package com.cqc.service.impl;

import com.cqc.dao.db2.User2Mapper;
import com.cqc.entity.User1EB;
import com.cqc.dao.db1.User1Mapper;
import com.cqc.entity.User2EB;
import com.cqc.service.User1Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqc.service.User2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mike
 * @since 2023-09-25
 */
/**
 * 注意： 引入atomikos的情况下，如果 tx1 service1的add()方法操作了2个数据库,如果 @Transactional指定了 transactionManager=tx1，那么只回滚tx1，不回滚tx2
 */
@Service
@Transactional(rollbackFor = Exception.class)//, transactionManager = "db1TransactionManager"
public class User1ServiceImpl extends ServiceImpl<User1Mapper, User1EB> implements User1Service {

    @Autowired
    User2Service user2Service;
    @Autowired
    User2Mapper user2Mapper;

    @Override
    public void addUser1AndUser2() {
        User1EB eb1 = new User1EB();
        eb1.setUsername("刘亦菲");
        eb1.setBirthday(LocalDateTime.now());
        eb1.setSex("1");
        eb1.setAddress("武汉");

        User2EB eb2 = new User2EB();
        eb2.setUsername("刘诗诗");
        eb2.setBirthday(LocalDateTime.now());
        eb2.setSex("1");
        eb2.setAddress("北京");


        baseMapper.insert(eb1);
        user2Mapper.insert(eb2);
        int a = 5 / 0;
    }

    @Override
    public void addUser() {
        User1EB eb1 = new User1EB();
        eb1.setUsername("刘亦菲");
        eb1.setBirthday(LocalDateTime.now());
        eb1.setSex("1");
        eb1.setAddress("武汉");
        baseMapper.insert(eb1);
    }
}
